<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 为了 128 二进制是  10000000  如果二进制达不到8位  则在前面加0补齐
    function add_0(arr,n) {
      for (let i = 0; i < n; i++) {
        arr.unshift('0')
      }
      return arr
    }
    function convertToBinary(num) {
      num = num.toString(2)
      return num.length > 8 ? num : add_0(num.split(''),(8 - num.length)).join('')
    }
    console.log(convertToBinary(10));
  </script>
</body>

</html>